All files / web/src/app/api/family/children/[playerId]/code route.ts

0% Statements 0/93
0% Branches 0/1
0% Functions 0/1
0% Lines 0/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                                                                                                                                                                           
import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import {
  FAMILY_CODE_EXPIRY_DAYS,
  getLinkedParentIds,
  getOrCreateFamilyCode,
  isParentOf,
  MAX_PARENTS_PER_CHILD,
  regenerateFamilyCode,
} from '@/lib/classroom'
import { getUserId } from '@/lib/viewer'

/**
 * GET /api/family/children/[playerId]/code
 * Get family code for a child (must be parent)
 *
 * Returns: { familyCode: string }
 */
export const GET = withAuth(async (_request, { params }) => {
  try {
    const { playerId } = (await params) as { playerId: string }
    const userId = await getUserId()

    // Verify user is a parent of this child
    const parentCheck = await isParentOf(userId, playerId)
    if (!parentCheck) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const [codeResult, linkedParentIds] = await Promise.all([
      getOrCreateFamilyCode(playerId),
      getLinkedParentIds(playerId),
    ])

    if (!codeResult) {
      return NextResponse.json({ error: 'Player not found' }, { status: 404 })
    }

    const { familyCode, generatedAt } = codeResult
    const expiresAt = generatedAt
      ? new Date(generatedAt.getTime() + FAMILY_CODE_EXPIRY_DAYS * 24 * 60 * 60 * 1000)
      : null

    return NextResponse.json({
      familyCode,
      generatedAt: generatedAt?.toISOString() ?? null,
      expiresAt: expiresAt?.toISOString() ?? null,
      linkedParentCount: linkedParentIds.length,
      maxParents: MAX_PARENTS_PER_CHILD,
    })
  } catch (error) {
    console.error('Failed to get family code:', error)
    return NextResponse.json({ error: 'Failed to get family code' }, { status: 500 })
  }
})

/**
 * POST /api/family/children/[playerId]/code
 * Regenerate family code for a child (invalidates old code)
 *
 * Returns: { familyCode: string }
 */
export const POST = withAuth(async (_request, { params }) => {
  try {
    const { playerId } = (await params) as { playerId: string }
    const userId = await getUserId()

    // Verify user is a parent of this child
    const parentCheck = await isParentOf(userId, playerId)
    if (!parentCheck) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const familyCode = await regenerateFamilyCode(playerId, userId)

    if (!familyCode) {
      return NextResponse.json({ error: 'Player not found' }, { status: 404 })
    }

    // Regeneration always sets a fresh timestamp
    const now = new Date()
    const expiresAt = new Date(now.getTime() + FAMILY_CODE_EXPIRY_DAYS * 24 * 60 * 60 * 1000)

    return NextResponse.json({
      familyCode,
      generatedAt: now.toISOString(),
      expiresAt: expiresAt.toISOString(),
    })
  } catch (error) {
    console.error('Failed to regenerate family code:', error)
    return NextResponse.json({ error: 'Failed to regenerate family code' }, { status: 500 })
  }
})